import LogicFlow, {
  BaseNodeModel,
  type ConnectRule,
  CircleNodeModel,
  CircleNode,
  h,
  RectNode,
  RectNodeModel,
  PolygonNode,
  PolygonNodeModel,
  GraphModel
} from '@logicflow/core'

import { type nodeProperty } from './type'
import ResizableRect from "./ResizableRect";
import MyGroup from "./MyGroup";

export default function RegisteNode(lf: LogicFlow) {
  // class ApplyNodeModel extends CircleNodeModel {
  //   // getConnectedTargetRules 获取当前节点作为边的起始节点规则
  //   getConnectedTargetRules(): ConnectRule[] {
  //     const rules = super.getConnectedTargetRules();
  //     const geteWayOnlyAsTarget = {
  //       message: '开始节点只能连出，不能连入！',
  //       validate: (source: BaseNodeModel, target: BaseNodeModel) => {
  //         let isValid = true;
  //         if (target) {
  //           isValid = false;
  //         }
  //         return isValid;
  //       },
  //     };
  //     // @ts-ignore
  //     rules.push(geteWayOnlyAsTarget);
  //     return rules;
  //   }
  // }
  // lf.register({
  //   type: 'apply',
  //   view: CircleNode,
  //   model: ApplyNodeModel,
  // })

  class ApproverNode0 extends RectNode {
    static extendKey = 'UserTaskNode';

    getLabelShape() {
      const {
        x,
        y,
        width,
        height,
        properties,
      } = this.props.model;

      const { labelColor, approveTypeLabel } = properties as nodeProperty;

      return h(
        'text',
        {
          fill: labelColor,
          fontSize: 12,
          x: x - width / 2 + 5,
          y: y - height / 2 + 15,
          width: 50,
          height: 25
        },
        approveTypeLabel,
      );
    }
    getShape() {
      const {
        x,
        y,
        width,
        height,
        radius,
      } = this.props.model;

      const style = this.props.model.getNodeStyle();

      return h(
        'g',
        {
        },
        [
          h(
            'rect',
            {
              ...style,
              x: x - width / 2,
              y: y - height / 2,
              rx: radius,
              ry: radius,
              width,
              height,
            },
          ),
          this.getLabelShape(),
        ],
      );
    }
  }
  class ApproverModel0 extends RectNodeModel {
    constructor(data: any, graphModel: GraphModel) {
      super(data, graphModel);
      this.width = 160;
      this.height = 40;
      this.properties = {
        labelColor: '#000000',
        approveTypeLabel: '',
        approveType: ''
      }
    }

    // getConnectedTargetRules 获取当前节点作为边的起始节点规则
    getConnectedTargetRules(): ConnectRule[] {
      const rules = super.getConnectedTargetRules();
      const geteWayOnlyAsTarget = {
        message: '开始节点只能连出，不能连入！',
        validate: (source: BaseNodeModel, target: BaseNodeModel) => {
          let isValid = true;
          if (target) {
            isValid = false;
          }
          return isValid;
        },
      };
      // @ts-ignore
      rules.push(geteWayOnlyAsTarget);
      return rules;
    }

    getNodeStyle() {
      const style = super.getNodeStyle();
      style.stroke = "#6495ED";
      style.strokeDasharray = "3 3";
      return style;
    }
  }
  lf.register({
    type: 'approver0',
    view: ApproverNode0,
    model: ApproverModel0,
  })

  class ApproverNode extends RectNode {
    static extendKey = 'UserTaskNode';

    getLabelShape() {
      const {
        x,
        y,
        width,
        height,
        properties,
      } = this.props.model;

      const { labelColor, approveTypeLabel } = properties as nodeProperty;

      return h(
        'text',
        {
          fill: labelColor,
          fontSize: 12,
          x: x - width / 2 + 5,
          y: y - height / 2 + 15,
          width: 50,
          height: 25
        },
        approveTypeLabel,
      );
    }
    getShape() {
      const {
        x,
        y,
        width,
        height,
        radius,
      } = this.props.model;

      const style = this.props.model.getNodeStyle();

      return h(
        'g',
        {
        },
        [
          h(
            'rect',
            {
              ...style,
              x: x - width / 2,
              y: y - height / 2,
              rx: radius,
              ry: radius,
              width,
              height,
            },
          ),
          this.getLabelShape(),
        ],
      );
    }
  }
  class ApproverModel extends RectNodeModel {
    constructor(data: any, graphModel: GraphModel) {
      super(data, graphModel);
      this.width = 160;
      this.height = 40;
      this.properties = {
        labelColor: '#000000',
        approveTypeLabel: '',
        approveType: ''
      }
    }

    // getConnectedTargetRules 获取当前节点作为边的起始节点规则
    getConnectedTargetRules(): ConnectRule[] {
      const rules = super.getConnectedTargetRules();
      const geteWayOnlyAsTarget = {
        message: '开始节点只能连出，不能连入！',
        validate: (source: BaseNodeModel, target: BaseNodeModel) => {
          let isValid = true;
          if (target) {
            isValid = false;
          }
          return isValid;
        },
      };
      // @ts-ignore
      rules.push(geteWayOnlyAsTarget);
      return rules;
    }

    getNodeStyle() {
      const style = super.getNodeStyle();
      style.stroke = "#3CB371";
      return style;
    }
  }
  lf.register({
    type: 'approver',
    view: ApproverNode,
    model: ApproverModel,
  })

  class ApproverNode2 extends RectNode {
    // 自定义节点样式属性
    static extendKey = 'UserTaskNode';
    getLabelShape() {
      const {
        x,
        y,
        width,
        height,
        properties,
      } = this.props.model;

      const { labelColor, approveTypeLabel } = properties as nodeProperty;

      return h(
        'text',
        {
          fill: labelColor,
          fontSize: 12,
          x: x - width / 2 + 5,
          y: y - height / 2 + 15,
          width: 50,
          height: 25
        },
        approveTypeLabel,
      );
    }
    getShape() {
      const {
        x,
        y,
        width,
        height,
        radius,
      } = this.props.model;

      const style = this.props.model.getNodeStyle();

      return h(
        'g',
        {
        },
        [
          h(
            'rect',
            {
              ...style,
              x: x - width / 2,
              y: y - height / 2,
              rx: radius,
              ry: radius,
              width,
              height,
            },
          ),
          this.getLabelShape(),
        ],
      );
    }
  }
  class ApproverModel2 extends RectNodeModel {
    // 自定义节点的形状属性
    constructor(data: any, graphModel: GraphModel) {
      super(data, graphModel);

      this.width = 160;
      this.height = 40;
      this.properties = {
        labelColor: '#000000',
        approveTypeLabel: '',
        approveType: ''
      }
    }

    getNodeStyle() {
      const style = super.getNodeStyle();
      style.stroke = "#0000ff";
      style.strokeDasharray = "3 3";
      return style;
    }
  }
  lf.register({
    type: 'approver2',
    view: ApproverNode2,
    model: ApproverModel2,
  })

  class ApproverNode3 extends RectNode {
    // 自定义节点样式属性
    static extendKey = 'UserTaskNode';
    getLabelShape() {
      const {
        x,
        y,
        width,
        height,
        properties,
      } = this.props.model;

      const { labelColor, approveTypeLabel } = properties as nodeProperty;

      return h(
        'text',
        {
          fill: labelColor,
          fontSize: 12,
          x: x - width / 2 + 5,
          y: y - height / 2 + 15,
          width: 50,
          height: 25
        },
        approveTypeLabel,
      );
    }
    getShape() {
      const {
        x,
        y,
        width,
        height,
        radius,
      } = this.props.model;

      const style = this.props.model.getNodeStyle();

      return h(
        'g',
        {
        },
        [
          h(
            'rect',
            {
              ...style,
              x: x - width / 2,
              y: y - height / 2,
              rx: radius,
              ry: radius,
              width,
              height,
            },
          ),
          this.getLabelShape(),
        ],
      );
    }
  }
  class ApproverModel3 extends RectNodeModel {
    // 自定义节点的形状属性
    constructor(data: any, graphModel: GraphModel) {
      super(data, graphModel);

      this.width = 160;
      this.height = 40;
      this.properties = {
        labelColor: '#000000',
        approveTypeLabel: '',
        approveType: ''
      }
    }

    getNodeStyle() {
      const style = super.getNodeStyle();
      style.stroke = "#FF6347";
      return style;
    }

    getConnectedSourceRules(): ConnectRule[] {
      const rules = super.getConnectedSourceRules();
      const geteWayOnlyAsTarget = {
        message: '结束节点只能连入，不能连出！',
        validate: (source: BaseNodeModel) => {
          let isValid = true;
          if (source) {
            isValid = false;
          }
          return isValid;
        },
      };
      // @ts-ignore
      rules.push(geteWayOnlyAsTarget);
      return rules;
    }
  }
  lf.register({
    type: 'approver3',
    view: ApproverNode3,
    model: ApproverModel3,
  })

  lf.register(ResizableRect)
  lf.register(MyGroup)

  // class JugementModel extends PolygonNodeModel {
  //   constructor(data: any, graphModel: GraphModel) {
  //     super(data, graphModel);
  //     this.points = [
  //       [35, 0],
  //       [70, 35],
  //       [35, 70],
  //       [0, 35],
  //     ];
  //     this.properties = {
  //       api: '',
  //     }
  //   }
  // }
  // lf.register({
  //   type: 'jugement',
  //   view: PolygonNode,
  //   model: JugementModel,
  // });

  // class FinshNodeModel extends CircleNodeModel {
  //   getConnectedSourceRules(): ConnectRule[] {
  //     const rules = super.getConnectedSourceRules();
  //     const geteWayOnlyAsTarget = {
  //       message: '结束节点只能连入，不能连出！',
  //       validate: (source: BaseNodeModel) => {
  //         let isValid = true;
  //         if (source) {
  //           isValid = false;
  //         }
  //         return isValid;
  //       },
  //     };
  //     // @ts-ignore
  //     rules.push(geteWayOnlyAsTarget);
  //     return rules;
  //   }
  // }
  // lf.register({
  //   type: 'finsh',
  //   view: CircleNode,
  //   model: FinshNodeModel,
  // })
}
